package com.adream.scalpel.app.client.ui;

import com.adream.scalpel.app.client.service.UserManagement;
import com.adream.scalpel.app.client.service.UserManagementAsync;
import com.adream.scalpel.common.client.vo.UserVO;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class UserEditDialogBox extends DialogBox {

    private Button cancelButton;
    private Button submitButton;
    private TextBox address3TextBox;
    private TextBox address2TextBox;
    private TextBox address1TextBox;
    private TextBox displayNameTextBox;
    private TextBox lastNameTextBox;
    private Label lastNameLabel;
    private Label address3Label;
    private Label address2Label;
    private Label address1Label;
    private Label displayNameLabel;
    private TextBox firstNameTextBox;
    private Label firstNameLabel;
    private TextBox userNameTextBox;
    private Label userNameLabel;
    private FlexTable flexTable;
    private SearchUsers searchUsers;

    public UserEditDialogBox(SearchUsers searchUsers) {
        this.searchUsers = searchUsers;
        flexTable = new FlexTable();
        setWidget(flexTable);

        flexTable.setSize("434px", "175px");

        userNameLabel = new Label("User Name");
        flexTable.setWidget(0, 0, userNameLabel);
        flexTable.getCellFormatter().setWidth(0, 0, "30%");
        flexTable.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);

        firstNameLabel = new Label("First Name");
        flexTable.setWidget(1, 0, firstNameLabel);
        flexTable.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);

        lastNameLabel = new Label("Last Name");
        flexTable.setWidget(2, 0, lastNameLabel);
        flexTable.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_RIGHT);

        displayNameLabel = new Label("Display Name");
        flexTable.setWidget(3, 0, displayNameLabel);
        flexTable.getCellFormatter().setHorizontalAlignment(3, 0, HasHorizontalAlignment.ALIGN_RIGHT);

        address1Label = new Label("Address 1");
        flexTable.setWidget(4, 0, address1Label);
        flexTable.getCellFormatter().setHorizontalAlignment(4, 0, HasHorizontalAlignment.ALIGN_RIGHT);

        address2Label = new Label("Address 2");
        flexTable.setWidget(5, 0, address2Label);
        flexTable.getCellFormatter().setHorizontalAlignment(5, 0, HasHorizontalAlignment.ALIGN_RIGHT);

        address3Label = new Label("Address 3");
        flexTable.setWidget(6, 0, address3Label);
        flexTable.getCellFormatter().setHorizontalAlignment(6, 0, HasHorizontalAlignment.ALIGN_RIGHT);

        userNameTextBox = new TextBox();
        flexTable.setWidget(0, 1, userNameTextBox);
        flexTable.getCellFormatter().setWidth(0, 1, "70%");
        userNameTextBox.setWidth("100%");

        firstNameTextBox = new TextBox();
        flexTable.setWidget(1, 1, firstNameTextBox);
        firstNameTextBox.setWidth("100%");

        lastNameTextBox = new TextBox();
        flexTable.setWidget(2, 1, lastNameTextBox);
        lastNameTextBox.setWidth("100%");

        displayNameTextBox = new TextBox();
        flexTable.setWidget(3, 1, displayNameTextBox);
        displayNameTextBox.setWidth("100%");

        address1TextBox = new TextBox();
        flexTable.setWidget(4, 1, address1TextBox);
        address1TextBox.setWidth("100%");

        address2TextBox = new TextBox();
        flexTable.setWidget(5, 1, address2TextBox);
        address2TextBox.setWidth("100%");

        address3TextBox = new TextBox();
        flexTable.setWidget(6, 1, address3TextBox);
        address3TextBox.setWidth("100%");

        submitButton = new Button();
        flexTable.setWidget(7, 0, submitButton);
        submitButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                doOnClickSubmitButton(event);
            }
        });

        flexTable.getCellFormatter().setHorizontalAlignment(7, 0, HasHorizontalAlignment.ALIGN_CENTER);
        submitButton.setText("Submit");

        cancelButton = new Button();
        flexTable.setWidget(7, 1, cancelButton);
        cancelButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                doOnClickCancelButton(event);
            }
        });
        flexTable.getCellFormatter().setHorizontalAlignment(7, 1, HasHorizontalAlignment.ALIGN_CENTER);
        cancelButton.setText("Cancel");
    }

    protected void doOnClickCancelButton(final ClickEvent event) {
        this.hide();
    }

    protected void doOnClickSubmitButton(final ClickEvent event) {
        UserManagementAsync userManagementAsync = UserManagement.Util.getInstance();
        final UserVO user = new UserVO(userNameTextBox.getText(), firstNameTextBox.getText(),
                lastNameTextBox.getText(), displayNameTextBox.getText(), address1TextBox.getText(), address2TextBox
                        .getText(), address3TextBox.getText(), true);
        userManagementAsync.updateUser(user, new AsyncCallback<Void>() {

            public void onFailure(Throwable caught) {
                Window.alert("Failed to update the user");
            }

            public void onSuccess(Void result) {
                searchUsers.addOrUpdateSearchResult(user);
                Window.alert("User updated");
            }
        });

    }

    public void setUserAndShow(UserVO user) {
        setHTML("Edit " + user.getUserName());
        userNameTextBox.setText(user.getUserName());
        firstNameTextBox.setText(user.getFirstName());
        lastNameTextBox.setText(user.getLastName());
        displayNameTextBox.setText(user.getDisplayName());
        address1TextBox.setText(user.getAddress1());
        address2TextBox.setText(user.getAddress2());
        address3TextBox.setText(user.getAddress3());
        center();
    }
}
